package me.baicai.modules.belldandy.gcb.service.impl;

import me.baicai.modules.belldandy.gcb.domain.UserProject;
import me.baicai.modules.belldandy.gcb.repository.UserProjectRepository;
import me.baicai.modules.belldandy.gcb.service.UserProjectService;
import me.baicai.modules.belldandy.gcb.service.dto.UserProjectDTO;
import me.baicai.modules.belldandy.gcb.service.dto.UserProjectQueryCriteria;
import me.baicai.modules.belldandy.gcb.service.mapper.UserProjectMapper;
import me.baicai.utils.FileUtil;
import me.baicai.utils.PageUtil;
import me.baicai.utils.QueryHelp;
import me.baicai.utils.ValidationUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 超级大白菜
* @date 2020-04-01
*/
@Service
@CacheConfig(cacheNames = "userProject")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserProjectServiceImpl implements UserProjectService {

    private final UserProjectRepository userProjectRepository;

    private final UserProjectMapper userProjectMapper;

    public UserProjectServiceImpl(UserProjectRepository userProjectRepository, UserProjectMapper userProjectMapper) {
        this.userProjectRepository = userProjectRepository;
        this.userProjectMapper = userProjectMapper;
    }

    @Override
    @Cacheable
    public Map<String,Object> queryAll(UserProjectQueryCriteria criteria, Pageable pageable){
        Page<UserProject> page = userProjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userProjectMapper::toDto));
    }

    @Override
    @Cacheable
    public List<UserProjectDTO> queryAll(UserProjectQueryCriteria criteria){
        return userProjectMapper.toDto(userProjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "#p0")
    public UserProjectDTO findById(Integer pid) {
        UserProject userProject = userProjectRepository.findById(pid).orElseGet(UserProject::new);
        ValidationUtil.isNull(userProject.getPid(),"UserProject","pid",pid);
        return userProjectMapper.toDto(userProject);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public UserProjectDTO create(UserProject resources) {
        return userProjectMapper.toDto(userProjectRepository.save(resources));
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(UserProject resources) {
        UserProject userProject = userProjectRepository.findById(resources.getPid()).orElseGet(UserProject::new);
        ValidationUtil.isNull( userProject.getPid(),"UserProject","id",resources.getPid());
        userProject.copy(resources);
        userProjectRepository.save(userProject);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer pid) {
        userProjectRepository.deleteById(pid);
    }


    @Override
    public void download(List<UserProjectDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserProjectDTO userProject : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("提成百分比", userProject.getCommission());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
